<?php

    /**
     * Updater Remote
     *
     * @package      Core
     * @subpackage   Updater
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Updater_Remote
    {

    ////////////////////////////////////////////////////////////////////////////
    //                          Attributes                                    //
    ////////////////////////////////////////////////////////////////////////////
        protected static $_authorizedHosts  = array('192.168.1.22');
        protected static $_types            = array('Serum', 'Project');
        protected static $_params           = array();
    ////////////////////////////////////////////////////////////////////////////
    //                          Public Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       string      $end
         */
        public function __construct()
        {
            parent::__construct(parent::TYPE_SERUM);
        }
        
        public static function setParams($params)
        {
        	self::$_params = $params;
        }
        
        public static function getParam($param)
        {
        	if (isset(self::$_params[$param]))
        	   return self::$_params[$param];
        	else
        	   return null;
        }
        
        public static function initResponse()
        {
            $response   = array('error' => null, 
                                'result' => null);
            self::checkAuthorizations($response);
            
            return $response;
        }
        
        public static function isValidHost($host)
        {
        	if (in_array($host, self::$_authorizedHosts)) {
        		return true;
        	} else {
        		return false;
        	}
        }
        
        public static function checkAuthorizations(&$response)
        {
            try {
	            if (!self::isValidHost($_SERVER['REMOTE_ADDR'])) {
	                throw new Core_Exception('Authorization refused !');
	            } else if (!in_array(self::getParam('type'), self::$_types)) {
	                throw new Core_Exception('Unknown type !');
	            }
            } catch (Core_Exception $e) {
                $response['error'] = $e->getMessage();
            }
        }
        
        public static function getCurrentVersion()
        {
		    $response  = self::initResponse();
		    
		    if ($response['error'] === null) {
		        $updater = Core_Updater::getInstance(self::getParam('type'));
		        $response['result'] = '#'.$updater->getCurrentVersion();
		        $response['version_'.strtolower(self::getParam('type'))] = $updater->getCurrentVersion();
		        
                $type = self::getParam('type') == self::$_types[0] ? self::$_types[1] : self::$_types[0];
                $class = 'Core_Updater_'.ucfirst($type);
                $updater2 = new $class();
                $response['version_'.strtolower($type)] = $updater2->getCurrentVersion();
		    }
		    
		    return $response;
        }
        
        public static function migrate()
        {
            $response  = self::initResponse();
            if ($response['error'] === null) {
                try {
	                $svn = Core_Svn_Adapter::factory(self::getParam('svn_client'));
	
	                $svn->setLogin(self::getParam('svn_login'));
	                $svn->setPassword(self::getParam('svn_password'));
	                $svn->setSource(self::getParam('svn_source'));
	                $svn->setBinary(self::getParam('svn_binary'));
	                $svn->setDestination(Core_IO_Path::combine(WWW_PATH, strtolower(self::getParam('type'))));
	                $svn->update();
                } catch (Exception $e) {
                    $response['error'] = htmlspecialchars($e->getMessage(), ENT_QUOTES);
                }
            }
                
            if ($response['error'] === null) {
                $updater    = Core_Updater::getInstance(self::getParam('type'));
                $from       = $updater->getCurrentVersion();
                $to         = self::getParam('to') === null ? $updater->getLatestVersion() : self::getParam('to');
                			    
			    if ($updater->getLatestVersion() < $to) {
			        $response['error'] = 'Numéro de version invalide !';
			    }
			    if ($from == $to) {
                    $response['error'] = 'Vous êtes déjà à la version '.$to.' !';
			    }
            }
            
            if ($response['error'] === null) {
                try {
                    $updater->migrate($to);
        
                    $response['result'] = 'Migrate '.self::getParam('type').' from #'.$from.' to #'.$to;
                    
	                $response['version_'.strtolower(self::getParam('type'))] = $updater->getCurrentVersion();
	                
	                $type = self::getParam('type') == self::$_types[0] ? self::$_types[1] : self::$_types[0];
	                $class = 'Core_Updater_'.ucfirst($type);
	                $updater2 = new $class();
	                $response['version_'.strtolower($type)] = $updater2->getCurrentVersion();
                    
                } catch (Doctrine_Migration_Exception $e) {
                    $response['error'] = htmlspecialchars($e->getMessage(), ENT_QUOTES);
                }
            }
            
            return $response;
        }
    }